//
//  SJZStatisticalViewController.m
//  SJZBusiness
//
//  Created by 彭利民 on 2018/4/26.
//  Copyright © 2018年 bwton. All rights reserved.
//

#import "SJZStatisticalViewController.h"
#import <iOS-Echarts.h>
#import "PYLineDemoOptions.h"
//#import "SJZStatisiticalTopView.h"
#import "SJZCouponModel.h"
#import "SJZCouponCell.h"
#import "YQLHttpHandler.h"

@interface SJZStatisticalViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) PYEchartsView *echartsView;

//@property (nonatomic, strong) SJZStatisiticalTopView *topView;
@property (strong, nonatomic) IBOutlet UIView *chartBgView;
@property (strong, nonatomic) IBOutlet UITextField *searchTextField;

@property (strong, nonatomic) UITableView *couponTableView;

@property (strong, nonatomic) NSMutableArray *chartDataArray;

@property (nonatomic, strong) NSMutableArray *couponArray;

@property (nonatomic, strong) PYOption *chartOption;

@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation SJZStatisticalViewController

- (PYEchartsView *)echartsView
{
    if (_echartsView == nil) {
        _echartsView = [[PYEchartsView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 188)];
        _echartsView.backgroundColor = [UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1];
    }
    
    return _echartsView;
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    if (self.couponTableView) {
        [self.couponTableView.mj_header beginRefreshing];
    }

    [self loadChartData];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"统计";
//     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"统计" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemClick)];
    
    [self configUI];

    
    self.currentPage = 1;
    [self.couponTableView.mj_header beginRefreshing];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
}

- (void)configUI{
    

    self.searchTextField.layer.cornerRadius = 3;
    self.searchTextField.layer.borderColor = kRGBA(245, 77, 83, 1).CGColor;
    self.searchTextField.layer.borderWidth = 2;
    self.searchTextField.layer.masksToBounds = YES;
    self.searchTextField.layer.cornerRadius = 3;
    self.searchTextField.layer.masksToBounds = YES;
    
    
    
    [self.view addSubview:self.couponTableView];
    
    //下拉刷新
    self.couponTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.currentPage = 1;
        [self loadDataWithPage:1 length:20];
        [self loadChartData];

    }];
    
    //上拉加载
    self.couponTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.currentPage ++;
        [self loadDataWithPage:self.currentPage length:20];
    }];
    
}

- (IBAction)searchAction:(UIButton *)sender {
    [self.view endEditing:YES];
//    [self searchVoucherWith:self.searchTextField.text];
    [self loadDataWithPage:self.currentPage length:20];

}

- (void)upDatChartWithTitle:(NSString *)chartTitle{
    
    PYOption *option = [PYLineDemoOptions basicAreaOption];
    if (option != nil) {
        self.chartOption = option;
        [self.echartsView setOption:option];
        [self.echartsView loadEcharts];
    }
    [self.chartBgView addSubview:self.echartsView];
    
    NSMutableArray *numArray = [NSMutableArray array];
    NSMutableArray *xArray = [NSMutableArray array];
    
    for (int i = 0; i<self.chartDataArray.count; i++) {
        NSDictionary *dict = self.chartDataArray[i];
        [numArray addObject:[NSNumber numberWithInteger:[dict[@"count"] integerValue]]];
        [xArray addObject:dict[@"week"]];
    }
    
    self.chartOption.addSeries([PYCartesianSeries initPYCartesianSeriesWithBlock:^(PYCartesianSeries *series) {
        series.dataEqual(numArray);
    }]);
 
    self.chartOption.addXAxis([PYAxis initPYAxisWithBlock:^(PYAxis *axis) {
        axis.typeEqual(PYAxisTypeCategory).boundaryGapEqual(@NO).addDataArr(xArray);
        
    }]);
    
    self.chartOption.titleEqual([PYTitle initPYTitleWithBlock:^(PYTitle *title) {
        title.textEqual(chartTitle);
    }]);
    
//    [self.echartsView removeFromSuperview];
//    self.echartsView = nil;
    [self.echartsView refreshEchartsWithOption:self.chartOption];
}

//加载图表内容
- (void)loadChartData{
    
    
    [YQLHttpHandler listStatConsumeVoucherWithuserId:[SJZAccountModel userFromArachiver].userId completion:^(NSArray *dictArray, NSString *dateRange, NSString *totalCount, NSString *errStr) {
        if (!errStr.length) {
            if (dictArray.count) {
                self.chartDataArray = [NSMutableArray arrayWithArray:dictArray];
            }
            [self.echartsView removeFromSuperview];
            self.echartsView = nil;
            [self upDatChartWithTitle:[NSString stringWithFormat:@"%@: %@",dateRange,totalCount]];
        }else{
            
        }
    }];
    
}


- (void)loadDataWithPage:(NSInteger)currentPage length:(NSInteger)length{
    
    [YQLHttpHandler listConsumeVoucherWithuserId:[SJZAccountModel userFromArachiver].userId page:[NSString stringWithFormat:@"%ld",currentPage] length:[NSString stringWithFormat:@"%ld",length] voucherCode:self.searchTextField.text completion:^(NSArray *voucherArray, NSString *errStr) {
        if (!errStr.length) {
            
            if (self.currentPage == 1) {
                [self.couponArray removeAllObjects];
            }
            
            if (voucherArray.count == length) {
                [self.couponArray addObjectsFromArray:voucherArray];
                [self.couponTableView reloadData];
                [self.couponTableView.mj_footer endRefreshing];
                
            }else{
                [self.couponArray addObjectsFromArray:voucherArray];
                [self.couponTableView reloadData];
                [self.couponTableView.mj_footer endRefreshingWithNoMoreData];
                
            }
        }else{
            [self.couponTableView.mj_footer endRefreshing];
        }
        [self.couponTableView.mj_header endRefreshing];
        
    }];
    
}


#pragma mark - tableViewDataSource  tableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.couponArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SJZCouponCell *couponCell = [SJZCouponCell cellWithTableView:tableView indexPath:indexPath];
    couponCell.couponModel = self.couponArray[indexPath.row];
    return couponCell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
    
}





- (void)leftItemClick
{
    NSLog(@"统计点击");
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
    
}

#pragma mark - lazyLoad
- (UITableView *)couponTableView{
    
    if (_couponTableView == nil) {
        _couponTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        _couponTableView.y = 304;
        _couponTableView.height = kScreenHeight - 304 - kNaviHeight - kTabbarHeight;
    }
    _couponTableView.delegate = self;
    _couponTableView.dataSource = self;
    return _couponTableView;
}

- (NSMutableArray *)couponArray{
    
    if (_couponArray == nil) {
        _couponArray = [NSMutableArray array];
    }
    
    return _couponArray;
}

- (NSMutableArray *)chartDataArray{
    
    if (_chartDataArray == nil) {
        _chartDataArray = [NSMutableArray array];
    }
    return _chartDataArray;
}












@end
